extends Node2D
class_name DamageText

## 伤害文本属性
@export var text: String = "0"
@export var is_crit: bool = false
@export var duration: float = 1.0
@export var float_speed: float = 50.0
@export var fade_speed: float = 2.0
@export var use_custom_style: bool = false
@export var custom_color: Color = Color.WHITE
@export var custom_font_size: int = 18

## 组件引用
@onready var label: Label = $Label
@onready var crit_icon: Sprite2D = $CritIcon

## 私有变量
var start_position: Vector2
var target_position: Vector2
var lifetime: float = 0.0

func _ready():
	# 设置文本
	if label:
		label.text = text
		# 样式
		if use_custom_style:
			label.modulate = custom_color
			label.add_theme_font_size_override("font_size", custom_font_size)
			if crit_icon:
				crit_icon.visible = false
		else:
			# 根据是否为暴击设置不同样式
			if is_crit:
				# 暴击效果：红色文字，更大字体
				label.modulate = Color.RED
				label.add_theme_font_size_override("font_size", 28)
				# 显示暴击图标
				if crit_icon:
					crit_icon.visible = true
					create_crit_icon_texture()
					# 暴击图标动画效果
					play_crit_animation()
			else:
				# 普通伤害：白色文字
				label.modulate = Color.WHITE
				label.add_theme_font_size_override("font_size", 18)
				# 隐藏暴击图标
				if crit_icon:
					crit_icon.visible = false
	
	# 设置初始位置
	start_position = global_position
	target_position = start_position + Vector2(0, -float_speed * duration)
	
	# 设置生命周期
	lifetime = duration

func _process(delta):
	# 更新生命周期
	lifetime -= delta
	if lifetime <= 0:
		queue_free()
		return
	
	# 向上浮动
	var progress = 1.0 - (lifetime / duration)
	global_position = start_position.lerp(target_position, progress)
	
	# 淡出效果
	if label:
		var alpha = lifetime / duration
		label.modulate.a = alpha
	
	# 暴击图标淡出效果
	if crit_icon and crit_icon.visible:
		var alpha = lifetime / duration
		crit_icon.modulate.a = alpha

## 静态方法：创建伤害文本
static func create_damage_text(damage: int, pos: Vector2, is_critical: bool = false) -> DamageText:
	var damage_text_scene = load("res://scenes/damage_text.tscn")
	var damage_text = damage_text_scene.instantiate()
	
	# 设置属性
	damage_text.text = str(damage)
	damage_text.is_crit = is_critical
	damage_text.global_position = pos
	
	return damage_text

## 自定义文本（用于x2/x3提示）
static func create_custom_text(text_value: String, pos: Vector2, color: Color, dur: float = 0.8, font_size: int = 22) -> DamageText:
	var damage_text_scene = load("res://scenes/damage_text.tscn")
	var node: DamageText = damage_text_scene.instantiate()
	node.text = text_value
	node.is_crit = false
	node.global_position = pos
	node.use_custom_style = true
	node.custom_color = color
	node.custom_font_size = font_size
	node.duration = dur
	return node

## 创建暴击图标纹理
func create_crit_icon_texture():
	if not crit_icon:
		return
	
	# 创建王者荣耀风格的暴击图标（红色星形）
	var texture = ImageTexture.new()
	var image = Image.create(32, 32, false, Image.FORMAT_RGBA8)
	
	# 绘制红色星形图标
	draw_star_icon(image, Color.RED)
	
	texture.set_image(image)
	crit_icon.texture = texture
	crit_icon.scale = Vector2(0.8, 0.8)

## 绘制星形图标
func draw_star_icon(image: Image, color: Color):
	var center = Vector2(16, 16)
	var outer_radius = 12.0
	var inner_radius = 6.0
	var points = 5
	
	# 绘制星形
	for i in range(points * 2):
		var angle = (i * PI) / points
		var radius = outer_radius if i % 2 == 0 else inner_radius
		var x = int(center.x + cos(angle) * radius)
		var y = int(center.y + sin(angle) * radius)
		
		if i > 0:
			# 绘制线条连接点
			var prev_angle = ((i - 1) * PI) / points
			var prev_radius = outer_radius if (i - 1) % 2 == 0 else inner_radius
			var prev_x = int(center.x + cos(prev_angle) * prev_radius)
			var prev_y = int(center.y + sin(prev_angle) * prev_radius)
			
			draw_line_on_image(image, Vector2(prev_x, prev_y), Vector2(x, y), color)

## 在图像上绘制线条
func draw_line_on_image(image: Image, start: Vector2, end: Vector2, color: Color):
	var steps = int(start.distance_to(end))
	for i in range(steps + 1):
		var t = float(i) / float(steps)
		var pos = start.lerp(end, t)
		if pos.x >= 0 and pos.x < image.get_width() and pos.y >= 0 and pos.y < image.get_height():
			image.set_pixel(int(pos.x), int(pos.y), color)

## 播放暴击动画效果
func play_crit_animation():
	if not crit_icon:
		return
	
	# 缩放动画：从小到大再回到正常大小
	var tween = create_tween()
	tween.tween_property(crit_icon, "scale", Vector2(1.2, 1.2), 0.1)
	tween.tween_property(crit_icon, "scale", Vector2(0.8, 0.8), 0.1)
	
	# 旋转动画
	var rotate_tween = create_tween()
	rotate_tween.tween_property(crit_icon, "rotation", PI * 2, 0.3)
